package com.example.flshtarvel.controller;

import com.example.flshtarvel.component.DownLoadImage;
import com.example.flshtarvel.component.UploadImage;
import com.example.flshtarvel.entity.*;
import com.example.flshtarvel.service.ArticleLikeMapService;
import com.example.flshtarvel.service.ArticleService;
import com.example.flshtarvel.service.ImageService;
import com.example.flshtarvel.service.TagArticleMapService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpSession;
import java.util.*;

@RestController
public class ArticleController {

    @Autowired
    ArticleService articleService;
    @Autowired
    ImageService imageService;
    @Autowired
    TagArticleMapService tagArticleMapService;
    @Autowired
    ArticleLikeMapService articleLikeMapService;

    @GetMapping("/article/{articleId}")
    public Map<String,Object> selectArticleById(@PathVariable("articleId") String articleId, HttpSession session){


        System.out.println(articleId);
        System.out.println(session.getAttribute("userId"));

        Map<String,Object> map = new HashMap<String,Object>();
        //表单数据
        Article articleResult = articleService.selectArticleById(articleId);
        if(articleResult==null){
            map.put("msg",0);
        }else{
            map.put("msg",1);
            map.put("article",articleResult);
            //标签数据
            List<Tag> tagList = tagArticleMapService.selectTagByArticleId(articleId);
            map.put("tagList",tagList);
            //用户是否点赞
            int isLike =  articleLikeMapService.selectArticleLikeByArticleIdAndUserId(articleId,(String)session.getAttribute("userId"));
            map.put("isLike",isLike);
        }
        return map;
    }

    @GetMapping("/article")
    public Map<String,Object> selectAllArticle(
                                               @RequestParam("articleTypeName") String articleTypeName,
                                               @RequestParam(name="page",defaultValue = "1") int page,
                                               @RequestParam(name="size",defaultValue = "10") int size){
        Map<String,Object> map = new HashMap<String,Object>();
        PageResult pageResult = new PageResult();
        List<Article> articleList = articleService.selectAllArticleByArticleTypeName(articleTypeName,page,size);
        if(articleList.isEmpty()){
            map.put("msg",0);


        }else{
            map.put("msg",1);
            map.put("articleList",articleList);

        }
        int count = articleService.selectArticleCountByArticleTypeName(articleTypeName);
        pageResult.setCurrentPage(page);
        pageResult.setCount(count);
        pageResult.setPageCount(count,size);
        pageResult.setPageSize(size);
        map.put("pageResult",pageResult);
        return map;
    }

    @GetMapping("/article/selectArticleByName")
    public Map<String,Object> selectArticleByName(
                                                  @RequestParam("articleTypeId") String articleTypeId,
                                                  @RequestParam("articleName") String articleName){
        Map<String,Object> map = new HashMap<String,Object>();
        List<Article> articleList = articleService.selectArticleByName(articleTypeId,articleName);
        if(articleList.isEmpty()){
            map.put("msg",0);
        }else{
            map.put("msg",1);
            map.put("articleList",articleList);
        }
        return map;
    }

    @PostMapping("/article")
    public Map<String,Object> insertArticle(
                                            Article article,
                                            @RequestParam("tags") String tags,
                                            @RequestParam("images") MultipartFile[] files) throws Exception {
        Map<String,Object> map = new HashMap<String,Object>();
        //处理表单文本数据
        int tag = articleService.insertArticle(article);
        if(tag==1){
            map.put("article",article);
        }else if(tag==2){
            map.put("msg",0);
            map.put("message","已有该数据");
            return map;
        }else{
            map.put("msg",0);
            return map;
        }
        //处理标签数据
        if(!tags.isEmpty()) {
            String[] tagList = tags.split(";");
            for (String tagItem : tagList) {
                tagArticleMapService.insertTagArticleMap(tagItem, article.getArticleId());
            }
        }
        //处理文件数据
        UploadImage uploadImage = new UploadImage();
        uploadImage.executeUploadImage(files,article);



        map.put("msg",1);
        return map;
    }

    @PutMapping("/article")
    public Map<String,Object> updateArticle(
                                            Article article,
                                            @RequestParam("images") MultipartFile[] files,
                                            @RequestParam("tags") String tags,
                                            @RequestParam("imageIds") String imageIds,//需删除的图片id
                                            @RequestParam("imageNames") String imageNames) throws Exception {
        Map<String,Object> map = new HashMap<String,Object>();
        //处理表单文本数据
        int tag = articleService.updateArticle(article);
        if(tag==1){
            map.put("article",article);
        }else{
            map.put("msg",0);
            return map;
        }

        //处理标签数据
        //将原有标签全删除
        tagArticleMapService.deleteTagArticleMapByArticleId(article.getArticleId());
        //插入新标签
        if(!tags.isEmpty()){
            String[] tagList = tags.split(";");
            for(String tagItem : tagList){
                tagArticleMapService.insertTagArticleMap(tagItem,article.getArticleId());
            }
        }


        //处理图片数据
        //删除图片
        if(!imageIds.isEmpty()) {
            DownLoadImage downLoadImage = new DownLoadImage();
            Image image = new Image();
            //需删除图片的id，图片名
            System.out.println("单个删除图片");
            String[] imageId = imageIds.split(";");
            String[] imageName = imageNames.split(";");
            int imgLen = imageId.length;
            for (int i = 0; i < imgLen; i++) {
                image.setImageId(imageId[i]);
                image.setImageName(imageName[i]);
                downLoadImage.executeDownloadImage(image, article);
            }
        }
        //上传图片
        UploadImage uploadImage = new UploadImage();
        uploadImage.executeUploadImage(files,article);

        map.put("msg",1);
        return map;

    }

    @DeleteMapping("/article/{articleId}")
    public Map<String,Object> deleteArticle(@PathVariable("articleId") String articleId){
        Map<String,Object> map = new HashMap<String,Object>();
        //删除文章
        int tag = articleService.deleteArticle(articleId);
        if(tag==1){
            //map.put("msg",1);
        }else{
            map.put("msg",0);
        }
        //删除标签
        tagArticleMapService.deleteTagArticleMapByArticleId(articleId);

        //删除图片
        DownLoadImage downLoadImage = new DownLoadImage();
        downLoadImage.executeDownloadImageBatch(articleId);
        map.put("msg",1);
        return map;
    }


    //首页搜索接口
    @GetMapping("/search/article")
    public Map<String,Object> searchArticle(@RequestParam("search") String search){
        Map<String,Object> map = new HashMap<String,Object>() ;
        List<Article> articleList = new ArrayList<Article>();
        articleList = articleService.searchArticle(search);
        if(articleList.isEmpty()){
            map.put("msg",0);
        }else{
            map.put("msg",1);
            map.put("articleList",articleList);
        }

        return map;
    }

    //根据articleTypeId
    @GetMapping("/selectArticleByArticleTypeId/{articleTypeId}")
    public Map<String,Object> selectArticleByArticleTypeId(@PathVariable("articleTypeId")String articleTypeId,
                                                           @RequestParam(name="page",defaultValue = "1")int page,
                                                           @RequestParam(name="size",defaultValue = "10")int size){
        Map<String,Object> map = new HashMap<>();
        PageResult pageResult = new PageResult();
        List<Article> articleList = articleService.selectAllArticleByArticleTypeId(articleTypeId,page,size);
        if(articleList.size()<=0){
            map.put("msg",0);
        }else{
            map.put("msg",1);
            map.put("articleList",articleList);
            int count = articleService.selectArticleCountByArticleTypeId(articleTypeId);
            pageResult.setCurrentPage(page);
            pageResult.setCount(count);
            pageResult.setPageCount(count,size);
            pageResult.setPageSize(size);
            map.put("pageResult",pageResult);
        }
        return map;
    }

}
